package com.cbs.android.component.sms.mob;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Handler;

import com.cbs.android.component.log.L;
import com.cbs.android.component.sms.smsinterface.SmsHandler;
import com.cbs.android.component.sms.smsinterface.SmsInterface;

import org.json.JSONException;
import org.json.JSONObject;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;


/**
 * Created by broche on 10/31/15.
 */
public class Mob implements SmsInterface {
    private static final String TAG = Mob.class.getName();

    public Mob(Context context) {
        try {
            ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            String ak = applicationInfo.metaData.getString("MOB_APPKEY");
            String sk = applicationInfo.metaData.getString("MOB_SECKEY");
            SMSSDK.initSDK(context, ak, sk);
        } catch (PackageManager.NameNotFoundException e) {
            L.e(TAG, "", e);
        }
    }

    @Override
    public void getVerifyCode(String phone, SmsHandler handler) {
        SMSSDK.unregisterAllEventHandler();
        SMSSDK.registerEventHandler(new MyEventHandler(handler)); //注册短信回调
        SMSSDK.getVerificationCode("86", phone);
    }

    private static class MyEventHandler extends EventHandler {
        private SmsHandler handler;
        private MyHandler myHandler = new MyHandler();

        public MyEventHandler(SmsHandler handler) {
            this.handler = handler;
        }

        @Override
        public void afterEvent(int event, int result, Object data) {
            Message message = new Message()
                    .setEvent(event)
                    .setResult(result)
                    .setData(data)
                    .setHandler(handler);
            myHandler.obtainMessage(0, message).sendToTarget();
        }

        private static class Message {
            private int event;
            private int result;
            private Object data;
            private SmsHandler handler;

            public int getEvent() {
                return event;
            }

            public Message setEvent(int event) {
                this.event = event;
                return this;
            }

            public int getResult() {
                return result;
            }

            public Message setResult(int result) {
                this.result = result;
                return this;
            }

            public Object getData() {
                return data;
            }

            public Message setData(Object data) {
                this.data = data;
                return this;
            }

            public SmsHandler getHandler() {
                return handler;
            }

            public Message setHandler(SmsHandler handler) {
                this.handler = handler;
                return this;
            }
        }

        private static class MyHandler extends Handler {
            @Override
            public void handleMessage(android.os.Message msg) {
                Message message = (Message) msg.obj;
                int result = message.getResult();
                int event = message.getEvent();
                SmsHandler handler = message.getHandler();
                Object data = message.getData();

                if (result == SMSSDK.RESULT_COMPLETE) {
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功
                    } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        //获取验证码成功
                        if (handler != null) {
                            handler.onSent();
                        }
                    } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                        //返回支持发送验证码的国家列表
                    }
                } else {
                    String errmsg = ((Throwable) data).getMessage();
                    try {
                        if (handler != null) {
                            JSONObject jsonObject = new JSONObject(errmsg);
                            int code = jsonObject.getInt("status");
                            handler.onFailure(code, StatusCode.getMessage(code));
                        }
                    } catch (JSONException e) {
                        if (handler != null) {
                            handler.onFailure(999, StatusCode.getMessage(999));
                        }
                    }
                }
            }
        }
    }
}
